package com.chapp.provider.handler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.chapp.base.Event;
import com.chapp.base.Venue;
import com.chapp.provider.DroidContentProvider;

public class EventHandler {

	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ DroidContentProvider.AUTHORITY + "/event");
	public static final String ID = "_id";
	public static final String TITLE = "title";
	public static final String ARTIST = "artist";
	public static final String VENUE_ID = "venue";
	public static final String IMAGE = "image";
	public static final String DESCRIPTION = "description";
	public static final String TICKETS = "tickets";
	public static final String DATE = "date";
	public static final String ID_EVENT = "id_event";
	

	public static Uri insertEvent(Context context, Event event) {
		String artists = getArtist(event.getArtists());
		VenueHandler.insertVenue(context, event.getVenue(), 1);

		ContentValues valores = new ContentValues();
		valores.put(ARTIST, artists);
		valores.put(DATE, event.getDate());
		valores.put(DESCRIPTION, event.getDescription());
		valores.put(IMAGE, event.getThumbnailURL());
		valores.put(TICKETS, event.getTicketsInfo());
		valores.put(TITLE, event.getTitle());
		valores.put(VENUE_ID, event.getVenue().getId());
		valores.put(ID_EVENT, event.getId());

		Uri uri = context.getContentResolver().insert(CONTENT_URI, valores);

		Log.i(uri.getPath(), uri.getPath());
		return uri;
	}

	private static String getArtist(List<String> artists) {
		String listArtist = "";
		Iterator<String> it = artists.iterator();
		while (it.hasNext()) {
			listArtist = it.next() + listArtist + "/";
		}
		return listArtist;
	}

	public static List<Event> getAllEvents(Context context) {
		Uri allEvents = CONTENT_URI;
		Cursor cursor = context.getContentResolver().query(allEvents, null,
				null, null, null);
		List<Event> list_events = new ArrayList<Event>();
		if (cursor.moveToFirst()) {
			do {
				Event event = new Event();
				event.setId(cursor.getString(cursor
						.getColumnIndex(EventHandler.ID_EVENT)));
				event.setArtists(setArtists(cursor.getString(cursor
						.getColumnIndex(EventHandler.ARTIST))));
				event.setDate(cursor.getString(cursor
						.getColumnIndex(EventHandler.DATE)));
				event.setDescription(cursor.getString(cursor
						.getColumnIndex(EventHandler.DESCRIPTION)));
				event.setThumbnailURL(cursor.getString(cursor
						.getColumnIndex(EventHandler.IMAGE)));
				event.setTicketsInfo(cursor.getString(cursor
						.getColumnIndex(EventHandler.TICKETS)));
				event.setTitle(cursor.getString(cursor
						.getColumnIndex(EventHandler.TITLE)));
				event.setVenue(setVenue(cursor.getString(cursor
						.getColumnIndex(EventHandler.VENUE_ID)), context));
				list_events.add(event);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list_events;
	}

	private static Venue setVenue(String string, Context context) {
		return VenueHandler.getVenue(context, string,1);
	}

	private static List<String> setArtists(String string) {
		List<String> artist_list = new ArrayList<String>();
		String[] artists = string.split("/");
		for (int i = 0; i < artists.length; i++) {
			artist_list.add(artists[i]);
		}
		return artist_list;
	}

	public static Event getEvent(Context context, String id) {
		Uri uri = Uri.withAppendedPath(CONTENT_URI, "" + id);
		Cursor cursor = context.getContentResolver().query(uri, null ,null, null, null);
		Event event = new Event();
		if (cursor.moveToFirst()) {
			event.setId(cursor.getString(cursor
					.getColumnIndex(EventHandler.ID_EVENT)));
			event.setArtists(setArtists(cursor.getString(cursor
					.getColumnIndex(EventHandler.ARTIST))));
			event.setDate(cursor.getString(cursor
					.getColumnIndex(EventHandler.DATE)));
			event.setDescription(cursor.getString(cursor
					.getColumnIndex(EventHandler.DESCRIPTION)));
			event.setThumbnailURL(cursor.getString(cursor
					.getColumnIndex(EventHandler.IMAGE)));
			event.setTicketsInfo(cursor.getString(cursor
					.getColumnIndex(EventHandler.TICKETS)));
			event.setTitle(cursor.getString(cursor
					.getColumnIndex(EventHandler.TITLE)));
			event.setVenue(setVenue(cursor.getString(cursor
					.getColumnIndex(EventHandler.VENUE_ID)), context));
			cursor.close();
			return event;
		}
		return null;
	}

	public static boolean deleteEvent(Context context, int id) {
		int rows = context.getContentResolver().delete(CONTENT_URI,
				ID_EVENT + " = " + id, null);
		if (rows != 0) {
			return true;
		}
		return false;
	}
}
